home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 3846 < prev    next >
Encoding:
Internet Message Format  |  1996-08-06  |  2.4 KB

  1. Path: news.compuserve.com!newsmaster
  2. From: 100754.2730@compuserve.com (Martin Aupperle)
  3. Newsgroups: comp.lang.c++
  4. Subject: Re: Q: Returning a reference
  5. Date: Fri, 26 Jan 1996 10:01:16 GMT
  6. Organization: CompuServe Incorporated
  7. Message-ID: <4ea94a$kq6@dub-news-svc-1.compuserve.com>
  8. References: <4cvsm2$5ig@dub-news-svc-4.compuserve.com>  <30FA95E1.50C@public.sta.net.cn> <4dp1a7$bo8@phoenix.dse.beckman.com>
  9. NNTP-Posting-Host: ad02-055.compuserve.com
  10. X-Newsreader: Forte Free Agent v0.56
  11.  
  12. jawalker@ccgate.dp.beckman.com (Jack Walker) wrote:
  13.  
  14. >Xu Ji <jafd@public.sta.net.cn> wrote:
  15.  
  16. >>Martin Aupperle wrote:
  17. >>: 
  18. >>: Borland C++ V4.5 does not allow to return a reference to a local
  19. >>: variable:
  20. >>: 
  21. >>: int &doIt() {
  22. >>: 
  23. >>:   int i = 7;
  24. >>:   return i;  // syntax error
  25. >>:   }
  26. >>: 
  27. >>: I remember that I once had a compiler that did allow it (it gave me
  28. >>: only a warning).
  29. >>: Which one is right? I think that it should be an error because after
  30. >>: the function has terminated, the reference has no data object it is
  31. >>: bound to any more.
  32. >>: 
  33. >>: Martin
  34. >>: 
  35. >>: -----------------------------------
  36. >>: Signatures are a waste of bandwidth
  37. >>: -----------------------------------
  38.  
  39. >>I think it should be an error, and I test it under Borland C++
  40. >>and Watcom C++, both this two compiler report it as an error.
  41.  
  42. >>but the following function can be compiled:
  43.  
  44. >> int &doIt()
  45. >> {
  46. >>     int i = 7;
  47. >>     return (int &)i;
  48.  
  49. You can do everything with typecasts. The compiler's type system is
  50. faked if you tell it explicitely that you have a type B at a storage
  51. location where you created an A in the first place. So if you
  52. EXPLICITELY  type cast to int &, it will be syntactically ok, of
  53. course (but it will crash anyway, what sheds a light on typecasts,
  54. IMO).
  55.  
  56. >> }
  57.  
  58. >>Best wishs,
  59. >>Xu yifeng
  60.  
  61. >Do you really want to return a reference to an object that is
  62. >allocated on the stack?  After doIt returns the space that i occupied
  63. >is available to be overwritten when the next stack frame is created.
  64.  
  65. No, of course not. But I want my compiler to flag such a thing as an
  66. error, if it happens occasionaly. Please note that if the function
  67. would have been declared
  68.  
  69.   int doit();
  70.  
  71. everything would be alright. It is easy to overlook the one character
  72. difference. 
  73.  
  74. What does the Standard say about references with a longer lifetime
  75. than the referenced object?
  76.  
  77. Martin
  78.  
  79.  
  80.  
  81.  
  82.  
  83.  
  84. -----------------------------------
  85. Signatures are a waste of bandwidth
  86. -----------------------------------
  87.  
  88.